# -*- coding:utf-8 -*- #

# ----------------------------------------------------------
# Name:        asyncDemo
# Description:
# Authon:      syl
# Date:        2020-12-09
# ----------------------------------------------------------
import time
import asyncio


async def handler(d):
    await asyncio.sleep(1)
    return d + 1


async def work(datas, flag):
    ts = time.time()
    for d in datas:
        r = await handler(d)
        print(f'{flag} handler {d}:{r}')
    return f'{flag} over!!{time.time() - ts}'
    pass


def main():
    datas = [i for i in range(100)]
    ts = time.time()
    datas = [datas[i * 10:(i + 1) * 10 if (i + 1) * 10 < 100 else 100] for i in range(10)]
    task = [work(d, i) for i, d in enumerate(datas)]
    loop = asyncio.get_event_loop()
    furures = loop.run_until_complete(asyncio.wait(task))
    for f in furures[0]:
        if f:
            print(f.result())
    print(f'exit!!! {time.time() - ts}')


if __name__ == '__main__':
    main()
